package com.ruoyi.common.core.mybatisplus.core;

import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ruoyi.common.constant.SearchParam;
import com.ruoyi.common.data.DataTable;
import com.ruoyi.common.data.DataTableLayUI;
import com.ruoyi.common.data.SearchCondition;
import com.ruoyi.common.utils.StringUtil;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 *
 * @author chen
 */
 public interface BaseService<T> extends IService<T> {

	default <K> T findById(Serializable id) {
		return getById(id);
	}


	/**
	 * 通过一列值查询
	 *
	 * @return
	 */

	default <K> List<T> selectByMap(Map<String, Object> params){
		return getBaseMapper().selectByMap(params);
	}

	/**
	 * 通过一唯一列值查询唯一值
	 *
	 * @param column
	 * @param value
	 * @return
	 */

	default <K> T selectByUniqueColumn(String column, Object value) {
		QueryWrapper<T> qw = new QueryWrapper<>();
		qw.eq(column,value);
		return getBaseMapper().selectOne(qw);
	}


	/**
	 * 是否加载 查询条件
	 *
	 * @param cnd
	 * @param k
	 * @param v
	 * @return
	 */
	default <K>  boolean idLoadCnd(String cnd, String k, Object v) {
		return k.startsWith(cnd) && null != v && v.toString().length() > 0;
	}

	/**
	 * 加载 搜索条件
	 *
	 * @param searchParams
	 * @param qw
	 */
	default <K> void loadSearchParam(Map<String, Object> searchParams, QueryWrapper qw) {
		if (null != searchParams) {
			searchParams.forEach((k, v) -> {
				if (idLoadCnd(SearchParam.SEARCH_EQ, k, v)) {
					qw.eq(StringUtil.underscoreName(k.split(SearchParam.SEARCH_EQ)[1]), v);
				} else if (idLoadCnd(SearchParam.SEARCH_LLIKE, k, v)) {
					qw.likeLeft(StringUtil.underscoreName(k.split(SearchParam.SEARCH_LLIKE)[1]),String.valueOf(v));
				} else if (idLoadCnd(SearchParam.SEARCH_RLIKE, k, v)) {
					qw.likeRight(StringUtil.underscoreName(k.split(SearchParam.SEARCH_RLIKE)[1]), String.valueOf(v));
				} else if (idLoadCnd(SearchParam.SEARCH_LIKE, k, v)) {
					qw.like(StringUtil.underscoreName(k.split(SearchParam.SEARCH_LIKE)[1]), String.valueOf(v));
				} else if (idLoadCnd(SearchParam.SEARCH_GE, k, v)) {//大于等于
					qw.ge(StringUtil.underscoreName(k.split(SearchParam.SEARCH_GE)[1]), String.valueOf(v));
				} else if (idLoadCnd(SearchParam.SEARCH_GT, k, v)) {//大于
					qw.gt(StringUtil.underscoreName(k.split(SearchParam.SEARCH_GT)[1]), String.valueOf(v));
				} else if (idLoadCnd(SearchParam.SEARCH_LE, k, v)) {//小于等于
					String ss = StringUtil.underscoreName(k.split(SearchParam.SEARCH_LE)[1]);
					System.out.println("ss:" + ss);
					String vv = String.valueOf(v);
					System.out.println("vv:" + vv);
					qw.le(ss, String.valueOf(v));
				} else if (idLoadCnd(SearchParam.SEARCH_LT, k, v)) {//小于
					qw.lt(StringUtil.underscoreName(k.split(SearchParam.SEARCH_LT)[1]), String.valueOf(v));
				}
			});
		}
	}

	/**
	 * 加载 排序条件
	 */
	default <K> void loadSort(Map<String, String> sorts, QueryWrapper qw) {
		if (null != sorts && sorts.size() > 0) {
			sorts.forEach((k, v) -> {
				if("asc".equals(v.toLowerCase())){
					qw.orderByAsc(k);
				}else{
					qw.orderByDesc(k);
				}
			});
		}
	}


	/**
	 * 分页 搜索
	 *
	 * @param condition
	 * @return
	 */

	default <K> DataTable<T> pageSearch(DataTable<T> condition) {
		Page<T> page = new Page<T>(condition.getPageNumber(), condition.getPageSize());
		SearchCondition cnd = SearchCondition.create();
		loadSearchParam(condition.getSearchParams(), cnd);
		loadSort(condition.getSorts(), cnd);
		page(page, cnd);
		condition.setTotal(page.getTotal());
		condition.setRows(page.getRecords());
		condition.setTotalPages(page.getPages());
		return condition;
	}
	/**
	 * 分页 搜索
	 *
	 * @param condition
	 * @return
	 */

	default <K> DataTableLayUI<T> pageSearchLayUI(DataTableLayUI<T> condition) {
		Page<T> page = new Page<T>(condition.getPageNum(), condition.getPageSize());
		SearchCondition cnd = SearchCondition.create();
		if(condition.getQw()!=null){
			loadSort(condition.getSorts(), condition.getQw());
			page(page, condition.getQw());
		}else{
			loadSearchParam(condition.getParams(), cnd);
			loadSort(condition.getSorts(), cnd);
			page(page, cnd);
		}
		condition.setTotal(page.getTotal());
		condition.setRows(page.getRecords());
		condition.setTotalPages(page.getPages());
		condition.setCode(HttpStatus.HTTP_OK);
		condition.setMsg("查询成功");
		return condition;
	}

	/**
	 * 分页 搜索
	 *
	 * @param condition
	 * @return
	 */

	default <K> DataTableLayUI<T> pageSearchCustom(Page<T> page, List<T> list, DataTableLayUI<T> condition) {
		condition.setTotal(page.getTotal());
		condition.setRows(list);
		condition.setTotalPages(page.getPages());
		condition.setCode(HttpStatus.HTTP_OK);
		condition.setMsg("查询成功");
		return condition;
	}

	default <K> Page<T> getPage(DataTableLayUI<T> condition) {
		Page<T> page = new Page<T>(condition.getPageNum(), condition.getPageSize());
		return page;
	}
}
